<template>
  <div class="navbar_box">
    <ul class="list">
      <li
        v-for="(val, index) in navbarlist"
        :key="index"
        :class="style == index ? 'li' : ''"
        @click="handleRouter(index)"
      >
        <p>{{ val.name }}</p>
        <span></span>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data () {
    return {
      style: 0,
      navbarlist: [
        {
          name: '个性推荐',
          path: '/home'
        },
        {
          name: '专属定制',
          path: '/home'
        },
        {
          name: '歌单',
          path: '/home'
        },
        {
          name: '排行榜',
          path: '/home/toplist'
        },
        {
          name: '歌手',
          path: '/home'
        },
        {
          name: '最新音乐',
          path: '/home'
        }
      ]
    }
  },
  mounted () {
    const id = sessionStorage.getItem('navId')
    if (id == '' || id == undefined) {
      this.style = 0
      return
    }
    this.style = id
  },
  beforeDestroy () {
    sessionStorage.removeItem('navId')
  },
  methods: {
    handleRouter (e) {
      this.style = e
      console.log(e)
      this.$router.push(this.navbarlist[e].path)
      sessionStorage.setItem('navId', `${e}`)
    }
  }
}
</script>

<style scoped>
.navbar_box {
  width: 100%;
  height: 60px;
  margin-left: 15px;
}
.navbar_box span {
  width: 50%;
  height: 5px;
  display: block;
  text-align: center;
  border-radius: 3px;
}
.list {
  width: 100%;
  height: 60px;
  display: flex;
  align-items: center;
}
.list li {
  width: 100px;
  height: 60px;
  text-align: center;
  display: flex;
  flex-direction: column;
  justify-content: center;
  font-size: 1.1rem;
}
.li p {
  font-size: 1.25rem;
  font-weight: 700;
}
.li span {
  background-color: #ec4141;
}
.list li span {
  position: relative;
  top: 5px;
  left: 50%;
  transform: translateX(-50%);
}
</style>
